package com.baizhi.springboot.config;

import com.alibaba.fastjson.JSONObject;
import com.baizhi.springboot.dao.LogDao;
import com.baizhi.springboot.entity.Log;
import com.baizhi.springboot.service.LogService;
import org.apache.activemq.command.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.TextMessage;

/**
 * 自定义配置类自动监听消费者MQ中的消息
 */
@Component
public class ConsumerListener {

    @Autowired
    private LogService logService;
    @JmsListener(destination = "ActiveMQ-Queue")
    public void consumer(Message message) {
        TextMessage textMessage = (TextMessage) message;
        try {
            String text = textMessage.getText();
            //System.out.println(text);
            Log log = JSONObject.parseObject(text,Log.class);
            //日志添加数据库中
            logService.insertLog(log);
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}
